package edu.fudan.service.impl;

import edu.fudan.dao.MeetingRoomDao;
import edu.fudan.model.MeetingRoom;
import edu.fudan.service.UserExistsException;
import edu.fudan.service.UserManager;
import edu.fudan.service.MeetingRoomManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.jws.WebService;
import java.util.List;


/**
 * Implementation of UserManager interface.
 *
 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
 */
@Service("meetingRoomManager")
@WebService(serviceName = "meetingRoom", endpointInterface = "edu.fudan.service.MeetingRoomService")
public class MeetingRoomManagerImpl extends GenericManagerImpl<MeetingRoom, Long> implements MeetingRoomManager {
    private MeetingRoomDao meetingRoomDao;

    @Autowired
    public void setMeetingRoomDao(MeetingRoomDao meetingRoomDao) {
        this.dao = meetingRoomDao;
        this.meetingRoomDao = meetingRoomDao;
    }

    /**
     * {@inheritDoc}
     */
    public MeetingRoom getMeetingRoom(String id) {
        return meetingRoomDao.get(new Long(id));
    }

    /**
     * {@inheritDoc}
     */
    public List<MeetingRoom> getMeetingRooms() {
        return meetingRoomDao.getAllDistinct();
    }

    /**
     * {@inheritDoc}
     */
    public MeetingRoom saveMeetingRoom(MeetingRoom meetingRoom) throws Exception {
        try {
            return meetingRoomDao.saveMeetingRoom(meetingRoom);
        } catch (Exception e) {
            //e.printStackTrace();
            log.warn(e.getMessage());
            throw new Exception("Meeting Room '" + meetingRoom.getName() + "' already exists!");
        }
    }

    /**
     * {@inheritDoc}
     */
    public void removeMeetingRoom(String id) {
        log.debug("removing meeting room: " + id);
        meetingRoomDao.remove(new Long(id));
    }

    /**
     * {@inheritDoc}
     *
     * @param username the login name of the human
     * @return User the populated user object
     * @throws UsernameNotFoundException thrown when username not found
     */
    public MeetingRoom getMeetingRoomByName(String name) throws Exception {
        return (MeetingRoom) meetingRoomDao.loadMeetingRoomByName(name);
    }
    
    /**
     * {@inheritDoc}
     */
    public List<MeetingRoom> search(String searchTerm) {
        return super.search(searchTerm, MeetingRoom.class);
    }
}
